#pragma once
#include<iostream>

//封装锁
namespace LockMutex
{
    class Mutex
    {
    public:
        //锁是不允许拷贝和赋值的
        Mutex(const Mutex&) = delete;
        const Mutex& operator=(const Mutex&) = delete;

        Mutex()
        {
            //初始化这个锁
            int n = pthread_mutex_init(&_lock,nullptr);
            (void)n;//不判断-也就是定义不用
        }
        ~Mutex()
        {
            //销毁已经初始化的互斥锁
            int n = pthread_mutex_destroy(&_lock);
            (void)n;
        }
        //加锁
        void Lock()
        {
            int n = pthread_mutex_lock(&_lock);
            (void)n;
        }
        //解锁
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_lock);
            (void)n;
        }
        pthread_mutex_t* LockPtr()
        {
            return &_lock;
        }
    private:
        pthread_mutex_t _lock;//锁
    };


    class mutex_lock
    {
    public:
        mutex_lock(Mutex& mutex):_mut(mutex)
        {
            _mut.Lock();
        }
        ~mutex_lock()
        {
            _mut.Unlock();
        }
    private:
        Mutex& _mut;
    };
}